package com.taobao.txc.common.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import com.taobao.txc.common.b.c;
import com.taobao.txc.common.util.b.a;
import com.taobao.txc.common.util.b.e;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.StringUtils;

public class ConsoleConfig
{
  private static final LoggerWrap a = LoggerInit.logger;
  private ScheduledExecutorService b = Executors.newSingleThreadScheduledExecutor();
  private String c = "https://cs2.gts.aliyuncs.com";
  private static ConsoleConfig d;
  private static String e = "txc.configserver.url";
  private static String f = "30000";
  private static long g = 10L;
  private static long h = 30000L;
  private static String i = "";
  private static int j = 0;
  private Random k = new Random();
  private ConcurrentHashMap<ConsoleConfig.KeyItem, Set<n>> l = new ConcurrentHashMap();
  private ConcurrentHashMap<ConsoleConfig.KeyItem, String> m = new ConcurrentHashMap();
  private ConcurrentHashMap<ConsoleConfig.KeyItem, ConsoleConfig.KeyItem> n = new ConcurrentHashMap();
  private ConcurrentHashMap<String, String> o = new ConcurrentHashMap();
  private ConcurrentHashMap<String, String> p = new ConcurrentHashMap();
  private ConcurrentHashMap<String, Set<m>> q = new ConcurrentHashMap();
  
  public Set<ConsoleConfig.KeyItem> a(Set<ConsoleConfig.KeyItem> paramSet)
  {
    HashSet localHashSet = new HashSet();
    Iterator localIterator = paramSet.iterator();
    while (localIterator.hasNext())
    {
      ConsoleConfig.KeyItem localKeyItem = (ConsoleConfig.KeyItem)localIterator.next();
      if (this.n.containsKey(localKeyItem)) {
        localHashSet.add(this.n.get(localKeyItem));
      } else {
        localHashSet.add(localKeyItem);
      }
    }
    return localHashSet;
  }
  
  public ConsoleConfig(String paramString)
  {
    this.c = paramString;
    g();
    d = this;
  }
  
  private ConsoleConfig()
  {
    g();
  }
  
  public static ConsoleConfig a()
  {
    if (d == null) {
      synchronized (ConsoleConfig.class)
      {
        if (d == null) {
          d = new ConsoleConfig();
        }
      }
    }
    return d;
  }
  
  public void b()
  {
    a.info(String.format("Use console config url:%s", new Object[] { this.c }));
  }
  
  private void f()
  {
    this.b.scheduleAtFixedRate(new b(this), 5000L, g, TimeUnit.MILLISECONDS);
  }
  
  private void g()
  {
    String str = System.getProperty(e);
    if (!StringUtils.isEmpty(str)) {
      this.c = str;
    }
    try
    {
      Class.forName("org.apache.http.client.HttpClient");
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      a.info("console config init error, no httpclient jar!");
      throw new RuntimeException(localClassNotFoundException);
    }
    synchronized (ConsoleConfig.class)
    {
      j += 1;
    }
    if (j > 1) {
      throw new RuntimeException(String.format("should create console only once! %s", new Object[] { this.c }));
    }
    f();
    b();
  }
  
  private void a(Map<String, String> paramMap, String paramString)
  {
    if (StringUtils.isEmpty(paramString)) {
      return;
    }
    StringTokenizer localStringTokenizer = new StringTokenizer(paramString, "\r\n");
    while (localStringTokenizer.hasMoreElements())
    {
      String str = localStringTokenizer.nextToken();
      if ((str != null) && (str.trim().length() != 0))
      {
        int i1 = str.indexOf('=');
        if (i1 <= 0) {
          throw new c("invalid txc rules", com.taobao.txc.common.b.b.j);
        }
        paramMap.put(str.substring(0, i1), str.substring(i1 + 1));
      }
    }
  }
  
  private boolean a(Map<String, String> paramMap1, Map<String, String> paramMap2, String paramString1, String paramString2, String paramString3)
  {
    boolean bool = false;
    if ((paramString2.indexOf("com.taobao.txc.rules") != -1) && (paramString1.equals("TXC_GROUP")))
    {
      bool = true;
      if (paramString2.equals("com.taobao.txc.rules.global")) {
        a(paramMap1, paramString3);
      } else {
        a(paramMap2, paramString3);
      }
    }
    return bool;
  }
  
  public void a(String paramString)
  {
    JSONArray localJSONArray = null;
    try
    {
      localJSONArray = JSON.parseArray(paramString);
      if (localJSONArray == null) {
        return;
      }
    }
    catch (Exception localException1)
    {
      a.a("poll Json parse failed ", String.format("contents:%s", new Object[] { paramString }), localException1);
      return;
    }
    HashMap localHashMap1 = new HashMap();
    HashMap localHashMap2 = new HashMap();
    for (int i1 = 0; i1 < localJSONArray.size(); i1++)
    {
      JSONObject localJSONObject = localJSONArray.getJSONObject(i1);
      if (localJSONObject != null)
      {
        String str1 = localJSONObject.getString("group");
        String str2 = localJSONObject.getString("dataId");
        String str3 = localJSONObject.getString("content");
        if (!StringUtils.isEmpty(str3))
        {
          str3 = StringUtils.trim(str3);
          ConsoleConfig.KeyItem localKeyItem1 = new ConsoleConfig.KeyItem(this, str1, str2);
          String str4 = localJSONObject.getString("version");
          if (!StringUtils.isEmpty(str4))
          {
            localKeyItem1.version = str4;
            this.n.put(localKeyItem1, localKeyItem1);
          }
          boolean bool = a(localHashMap1, localHashMap2, str1, str2, str3);
          if (!this.m.containsKey(localKeyItem1)) {
            synchronized (this.m)
            {
              if (!this.m.containsKey(localKeyItem1)) {
                this.m.put(localKeyItem1, str3);
              }
            }
          } else {
            if (StringUtils.equals((String)this.m.get(localKeyItem1), str3)) {
              continue;
            }
          }
          if (e.a(str1, str2, str3)) {
            this.m.put(localKeyItem1, str3);
          }
          a.info(String.format("received updated group:%s dataId:%s content:%s", new Object[] { str1, str2, str3 }));
          if ((!bool) && (!this.l.isEmpty()))
          {
            ??? = this.l.keySet().iterator();
            while (((Iterator)???).hasNext())
            {
              ConsoleConfig.KeyItem localKeyItem2 = (ConsoleConfig.KeyItem)((Iterator)???).next();
              if ((localKeyItem2 != null) && (localKeyItem2.dataId.equals(str2)) && (localKeyItem2.group.equals(str1)))
              {
                Set localSet = (Set)this.l.get(localKeyItem2);
                if (localSet != null)
                {
                  Iterator localIterator = localSet.iterator();
                  while (localIterator.hasNext())
                  {
                    n localn = (n)localIterator.next();
                    try
                    {
                      localn.a(str3);
                    }
                    catch (Exception localException2) {}
                  }
                }
              }
            }
          }
        }
      }
    }
    if ((!localHashMap1.isEmpty()) || (!localHashMap2.isEmpty())) {
      a(localHashMap1, localHashMap2);
    }
  }
  
  private void a(Map<String, String> paramMap1, Map<String, String> paramMap2)
  {
    HashSet localHashSet1 = new HashSet();
    HashSet localHashSet2 = new HashSet();
    Iterator localIterator1;
    String str1;
    String str2;
    Set localSet;
    Iterator localIterator2;
    m localm;
    if (!paramMap2.isEmpty())
    {
      localIterator1 = paramMap2.keySet().iterator();
      while (localIterator1.hasNext())
      {
        str1 = (String)localIterator1.next();
        str2 = (String)paramMap2.get(str1);
        if ((!StringUtils.isEmpty(str2)) && ((!this.o.containsKey(str1)) || (!((String)this.o.get(str1)).equals(str2))))
        {
          if (this.q.containsKey(str1))
          {
            localSet = (Set)this.q.get(str1);
            localIterator2 = localSet.iterator();
            while (localIterator2.hasNext())
            {
              localm = (m)localIterator2.next();
              try
              {
                localm.a(str2);
              }
              catch (Exception localException1) {}
            }
          }
          if (paramMap1.containsKey(str1)) {
            localHashSet1.add(str1);
          }
        }
      }
      localHashSet2.addAll(this.o.keySet());
      localHashSet2.removeAll(paramMap2.keySet());
      this.o.clear();
      this.o.putAll(paramMap2);
    }
    if (!paramMap1.isEmpty())
    {
      localIterator1 = paramMap1.keySet().iterator();
      while (localIterator1.hasNext())
      {
        str1 = (String)localIterator1.next();
        if ((!localHashSet1.contains(str1)) && (!this.o.containsKey(str1)))
        {
          str2 = (String)paramMap1.get(str1);
          if ((!StringUtils.isEmpty(str2)) && ((!this.p.containsKey(str1)) || (!((String)this.p.get(str1)).equals(str2)) || (localHashSet2.contains(str1)))) {
            if (this.q.containsKey(str1))
            {
              localSet = (Set)this.q.get(str1);
              localIterator2 = localSet.iterator();
              while (localIterator2.hasNext())
              {
                localm = (m)localIterator2.next();
                try
                {
                  localm.a(str2);
                }
                catch (Exception localException2) {}
              }
            }
          }
        }
      }
      this.p.clear();
      this.p.putAll(paramMap1);
    }
  }
  
  public String a(String paramString1, String paramString2)
  {
    if ((StringUtils.isEmpty(paramString1)) || (StringUtils.isEmpty(paramString2))) {
      return null;
    }
    ConsoleConfig.KeyItem localKeyItem = new ConsoleConfig.KeyItem(this, paramString2, paramString1);
    StringBuilder localStringBuilder = new StringBuilder();
    try
    {
      localStringBuilder.append(this.c).append("/cs/api/config.co?");
      localStringBuilder.append("group=").append(URLEncoder.encode(paramString2, "UTF-8"));
      localStringBuilder.append("&dataId=").append(URLEncoder.encode(paramString1, "UTF-8"));
      localStringBuilder.append("&timeout=").append(0);
      i1 = 30000;
      if (this.m.containsKey(localKeyItem)) {
        i1 = 5000;
      }
      String str2 = StringUtils.trim(com.taobao.txc.common.util.b.b.a(localStringBuilder.toString(), i1));
      if (StringUtils.isEmpty(str2))
      {
        a.info(String.format("dataid:%s,group:%s is empty", new Object[] { paramString1, paramString2 }));
        String str4 = str2;
        int i5;
        return str4;
      }
      if ((!this.m.containsKey(localKeyItem)) || (!StringUtils.equals((String)this.m.get(localKeyItem), str2))) {
        synchronized (this.m)
        {
          if (((!this.m.containsKey(localKeyItem)) || (!StringUtils.equals((String)this.m.get(localKeyItem), str2))) && (e.a(paramString2, paramString1, str2))) {
            this.m.put(localKeyItem, str2);
          }
        }
      }
    }
    catch (c localc)
    {
      int i1;
      if (localc.b() == com.taobao.txc.common.b.b.q)
      {
        a.warn(String.format("http get return 404 dataId:%s,group:%s", new Object[] { paramString1, paramString2 }));
        String str3 = null;
        int i4;
        return str3;
      }
      a.a(localc.getMessage(), String.format("GET dataId:%s group:%s", new Object[] { paramString1, paramString2 }), localc);
      if (!this.m.containsKey(localKeyItem)) {
        synchronized (this.m)
        {
          if (!this.m.containsKey(localKeyItem))
          {
            str5 = StringUtils.trim(e.a(paramString2, paramString1));
            a.info(String.format("GET loadCache dataId:%s group:%s value:%s", new Object[] { paramString1, paramString2, str5 }));
            if (!StringUtils.isEmpty(str5)) {
              this.m.put(localKeyItem, str5);
            }
          }
        }
      }
    }
    catch (Exception localException)
    {
      String str5;
      int i2;
      a.a(com.taobao.txc.common.b.b.o.be, String.format("GET dataId:%s group:%s", new Object[] { paramString1, paramString2 }), localException);
      if (!this.m.containsKey(localKeyItem)) {
        synchronized (this.m)
        {
          if (!this.m.containsKey(localKeyItem))
          {
            str5 = StringUtils.trim(e.a(paramString2, paramString1));
            a.info(String.format("GET loadCache dataId:%s group:%s value:%s", new Object[] { paramString1, paramString2, str5 }));
            if (!StringUtils.isEmpty(str5)) {
              this.m.put(localKeyItem, str5);
            }
          }
        }
      }
    }
    finally
    {
      int i3;
      int i6 = this.k.nextInt(100);
      try
      {
        Thread.sleep(100 + i6);
      }
      catch (InterruptedException localInterruptedException5) {}
    }
    String str1 = (String)this.m.get(localKeyItem);
    a.info(String.format("dataid:%s,group:%s,result:%s", new Object[] { paramString1, paramString2, str1 }));
    return str1;
  }
  
  public void a(String paramString1, String paramString2, n paramn)
  {
    if ((StringUtils.isEmpty(paramString1)) || (StringUtils.isEmpty(paramString2)) || (paramn == null)) {
      return;
    }
    ConsoleConfig.KeyItem localKeyItem = new ConsoleConfig.KeyItem(this, paramString2, paramString1);
    if (!this.l.containsKey(localKeyItem))
    {
      synchronized (this.l)
      {
        if (!this.l.containsKey(localKeyItem)) {
          this.l.put(localKeyItem, new HashSet());
        }
      }
      ??? = (Set)this.l.get(localKeyItem);
      ((Set)???).add(paramn);
    }
    else
    {
      a.info(String.format("ignore duplicated dataId:%s group:%s addListener", new Object[] { paramString1, paramString2 }));
    }
  }
  
  private void a(String paramString1, String paramString2, String paramString3, m paramm)
  {
    if ((StringUtils.isEmpty(paramString2)) || (StringUtils.isEmpty(paramString3))) {
      return;
    }
    ConsoleConfig.KeyItem localKeyItem = new ConsoleConfig.KeyItem(this, paramString3, paramString2);
    if (!this.l.containsKey(localKeyItem)) {
      synchronized (this.l)
      {
        if (!this.l.containsKey(localKeyItem)) {
          this.l.put(localKeyItem, new HashSet());
        }
      }
    }
    if (paramm != null)
    {
      if (!this.q.containsKey(paramString1)) {
        synchronized (this.q)
        {
          if (!this.q.containsKey(paramString1)) {
            this.q.put(paramString1, new HashSet());
          }
        }
      }
      ??? = (Set)this.q.get(paramString1);
      ((Set)???).add(paramm);
    }
  }
  
  public boolean a(String paramString1, String paramString2, String paramString3, a parama)
  {
    if ((StringUtils.isEmpty(paramString1)) || (StringUtils.isEmpty(paramString2)) || (StringUtils.isEmpty(paramString3))) {
      return false;
    }
    if ((parama == null) && (!StringUtils.equals(paramString1, "com.taobao.txc.alert.%s")))
    {
      a.warn(String.format("putSingle without security dataId:%s group:%s content:%s", new Object[] { paramString1, paramString2, paramString3 }));
      return false;
    }
    StringBuilder localStringBuilder = new StringBuilder();
    try
    {
      localStringBuilder.append(this.c).append("/cs/api/putSingle.json");
      HashMap localHashMap1 = new HashMap();
      localHashMap1.put("group", paramString2);
      localHashMap1.put("dataId", paramString1);
      localHashMap1.put("content", paramString3);
      HashMap localHashMap2 = null;
      if (parama != null)
      {
        localHashMap2 = new HashMap();
        if (parama.a() != null) {
          localHashMap2.put("CS-AK", parama.a());
        }
        if (parama.b() != null) {
          localHashMap2.put("CS-AppName", parama.b());
        }
        if (parama.d() != null) {
          localHashMap2.put("CS-Vgroup", parama.d());
        }
        if (parama.c() != null) {
          localHashMap2.put("CS-Digest", parama.c());
        }
      }
      com.taobao.txc.common.util.b.b.a(localStringBuilder.toString(), localHashMap2, JSON.toJSONString(localHashMap1));
      a.info(String.format("POST update dataId%s group:%s", new Object[] { paramString1, paramString2 }));
    }
    catch (Exception localException)
    {
      a.a(com.taobao.txc.common.b.b.o.be, String.format("POST update dataId:%s group:%s", new Object[] { paramString1, paramString2 }), localException);
      return false;
    }
    return true;
  }
  
  public String b(String paramString1, String paramString2)
  {
    String str1 = System.getProperty(paramString2);
    if (StringUtils.isNotEmpty(str1)) {
      return str1;
    }
    String str2 = b("global", paramString2, null);
    if ((StringUtils.isNotEmpty(paramString1)) && (!paramString1.equals("global")) && (!this.o.keySet().contains(paramString2))) {
      synchronized (this.o)
      {
        if (!this.o.keySet().contains(paramString2)) {
          b(paramString1, paramString2, null);
        }
      }
    }
    if (this.o.containsKey(paramString2))
    {
      ??? = (String)this.o.get(paramString2);
      if (!StringUtils.isEmpty((String)???)) {
        return ???;
      }
    }
    return str2;
  }
  
  public void a(String paramString1, String paramString2, m paramm)
  {
    String str1 = System.getProperty(paramString1);
    if (StringUtils.isNotEmpty(str1))
    {
      paramm.a(str1);
      return;
    }
    String str2 = b("global", paramString1, null);
    String str3 = null;
    if ((!StringUtils.isEmpty(paramString2)) && (!paramString2.equals("global"))) {
      str3 = b(paramString2, paramString1, paramm);
    }
    if (str3 != null) {
      paramm.a(str3);
    } else if (str2 != null) {
      paramm.a(str2);
    }
  }
  
  private String b(String paramString1, String paramString2, m paramm)
  {
    if ((StringUtils.isEmpty(paramString1)) || (StringUtils.isEmpty(paramString2))) {
      return null;
    }
    String str1 = null;
    String str2 = String.format("com.taobao.txc.rules.%s", new Object[] { paramString1 });
    try
    {
      ConcurrentHashMap localConcurrentHashMap = null;
      if (paramString1.equals("global")) {
        localConcurrentHashMap = this.p;
      } else {
        localConcurrentHashMap = this.o;
      }
      if (localConcurrentHashMap.containsKey(paramString2))
      {
        str1 = (String)localConcurrentHashMap.get(paramString2);
      }
      else
      {
        if (localConcurrentHashMap.isEmpty()) {
          synchronized (localConcurrentHashMap)
          {
            if (localConcurrentHashMap.isEmpty())
            {
              String str3 = a(str2, "TXC_GROUP");
              HashMap localHashMap = new HashMap();
              a(localHashMap, str3);
              localConcurrentHashMap.putAll(localHashMap);
            }
          }
        }
        if (!localConcurrentHashMap.containsKey(paramString2)) {
          synchronized (localConcurrentHashMap)
          {
            if (!localConcurrentHashMap.containsKey(paramString2)) {
              localConcurrentHashMap.put(paramString2, i);
            }
          }
        }
      }
      if (str1 == null) {
        str1 = (String)localConcurrentHashMap.get(paramString2);
      }
      a(paramString2, str2, "TXC_GROUP", paramm);
    }
    catch (Exception localException) {}
    return str1;
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.common.config.ConsoleConfig
 * JD-Core Version:    0.7.0.1
 */